﻿using PhotoGallery.Controllers;
using PhotoGallery.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using PhotoGallery.Data.Entity;
using PhotoGallery.Models.ViewModels;
using System.IO;
using Lib.Web.Mvc;

namespace PhotoGallery.Areas.Admin.Controllers
{
    public class GalleryController : BaseController
    {
        //
        // GET: /Admin/Gallery/

        public ActionResult Index()
        {
            //if (Request.IsAuthenticated)
            //{
            //    return RedirectToAction("List","Albums",null);
            //}
            return RedirectToAction("List");
        }

        public ActionResult List(int? page)
        {
            PublicAlbumsModel model = new PublicAlbumsModel();
            var albums = model.Get();

            var pageNumber = page ?? 1;
            var onePageOfProducts = albums.ToPagedList(pageNumber, Tune.ItemsPerPage -1);

            ViewBag.OnePageOfProducts = onePageOfProducts;
            return View(model.Get());
        }

        public ActionResult Preview(int? albumID)
        {
            if (!albumID.HasValue)
            {
                return RedirectToAction("Index");
            }
            if (Request.UrlReferrer != null)
            {
                ViewBag.backUrl = Request.UrlReferrer.ToString();
            }
            else
            {
                ViewBag.backUrl = Url.Action("Index");
            }
            PublicAlbumInfo album = new PublicAlbumsModel().Get(albumID ?? 1);
            return View(new PhotosModel(albumID ?? -1, album.Owner, album.FolderName).Get());
        }

        public ActionResult Show(int? id, int? albumID)
        {
            if (!albumID.HasValue)
            {
                return RedirectToAction("Index");
            }
            if (Request.UrlReferrer != null)
            {
                ViewBag.backUrl = Request.UrlReferrer.ToString();
            }
            else
            {
                ViewBag.backUrl = Url.Action("Index");
            }
            PublicAlbumInfo album = new PublicAlbumsModel().Get(albumID ?? 1);
        
            ViewBag.AlbumID = albumID;
            VideoModel model = new VideoModel(albumID ?? -1, album.Owner, album.FolderName);
            VideoViewModel viewModel = new VideoViewModel();
            viewModel.Video = model.Get(id ?? 1);
            if (viewModel.Video != null)
            {
                VideoAttacmentModel amodel = new VideoAttacmentModel(albumID ?? -1, viewModel.Video.ID, album.Owner, album.FolderName);
                viewModel.VideoAttachments = amodel.Get();
            }
            viewModel.Videos = model.Get();
            return View(viewModel);
        }

        public ActionResult Play(int? id, int? albumID)
        {
            if (!albumID.HasValue)
            {
                return RedirectToAction("Index");
            }
            if (Request.UrlReferrer != null)
            {
                ViewBag.backUrl = Request.UrlReferrer.ToString();
            }
            else
            {
                ViewBag.backUrl = Url.Action("Index");
            }
            PublicAlbumInfo album = new PublicAlbumsModel().Get(albumID ?? 1);

            ViewBag.AlbumID = albumID;
            AudioModel model = new AudioModel(albumID ?? default(int), album.Owner, album.FolderName);
            AudioViewModel viewModel = new AudioViewModel();
            viewModel.Audio = model.Get(id ?? 1);
            viewModel.AudioList = model.Get();
            return View(viewModel);
        }

        public ActionResult DownloadPhoto(string file = "")
        {

            var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            var mimeType = "image/jpeg";
            var fileDownloadName = Path.GetFileName(file);
            return File(fileStream, mimeType, fileDownloadName);
        }

        public ActionResult DownloadAudio(string file = "")
        {
            var fileStream = new FileStream(file, FileMode.Open, FileAccess.ReadWrite);
            var mimeType = "audio/mpeg";
            var fileDownloadName = Path.GetFileName(file);
            return File(fileStream, mimeType, fileDownloadName);
        }

        public ActionResult DownloadVideo(string file = "")
        {
            if (Request.UserAgent.Contains("MSIE"))
            {
                return DownloadVideoIE(file);
            }
            else
            {
                return DownloadChrom(file);
            }
        }

        private ActionResult DownloadVideoIE(string file = "")
        {
            var fileStream = new FileStream(file, FileMode.Open);
            var mimeType = "video/mp4";
            string ext = Path.GetExtension(file);

            var fileDownloadName = Path.GetFileName(file);
            switch (ext)
            {
                case ".webm":
                    mimeType = "video/webm";
                    return File(fileStream, mimeType, fileDownloadName);
                case ".ogv":
                    mimeType = "video/ogg";
                    return File(fileStream, mimeType, fileDownloadName);
                default:
                    mimeType = "video/mp4";
                    return File(fileStream, mimeType, fileDownloadName);
            }
        }

        private RangeFilePathResult DownloadChrom(string file = "")
        {
            string ext = Path.GetExtension(file);
            FileInfo oceansClipInfo = null;
            string type;
            switch (ext)
            {
                case ".webm":
                    type = "webm";
                    break;
                case ".ogv":
                    type = "ogg";
                    break;
                default:
                    type = "mp4";
                    break;
            }
            string oceansClipMimeType = String.Format("video/{0}", type);

            switch (ext)
            {
                case ".mp4":
                    oceansClipInfo = new FileInfo(file);
                    break;
                case ".webm":
                    oceansClipInfo = new FileInfo(file);
                    break;
                case ".ogg":
                    oceansClipInfo = new FileInfo(file);
                    break;
            }

            return new RangeFilePathResult(oceansClipMimeType, oceansClipInfo.FullName, oceansClipInfo.LastWriteTimeUtc, oceansClipInfo.Length);
        }

    }
}
